//
//  DetailViewService.swift
//  placepic
//
//  Created by 이유진 on 2020/07/15.
//  Copyright © 2020 elesahich. All rights reserved.
//

import Foundation
import Alamofire

struct DetailViewService {
    static let shared = DetailViewService()
    
    func getPlaces(_ queryurl: String,completion: @escaping (NetworkResult<Any>) -> Void) {
        print(#function)
        guard let token = UserDefaults.standard.string(forKey: "accessToken") else { return }
        let url = placeListAPI.getdetailplace + queryurl
        let header: HTTPHeaders = [
            "token": token,
            "Content-Type": "application/json"
        ]
        let dataRequest = Alamofire.request(url, method: .get, encoding:JSONEncoding.default, headers: header)
        
        dataRequest.responseData { dataResponse in
                switch dataResponse.result {
                case .success:
                    guard let statusCode = dataResponse.response?.statusCode else { return }
                    guard let value = dataResponse.result.value else { return }
                    let networkResult = self.judge(by: statusCode, value)
                    completion(networkResult)
                case .failure:
                    completion(.networkFail)
                }
            }
        }
        private func judge(by statusCode: Int, _ data: Data) -> NetworkResult<Any> {
            switch statusCode {
            case 200: return isDetailPlace(by: data)
            case 400: return .pathErr
            case 500: return .serverErr
            default: return .networkFail
            }
        }

        private func isDetailPlace(by data: Data) -> NetworkResult<Any> {
            let decoder = JSONDecoder()
            guard let decodedData = try? decoder.decode(DetailView.self, from: data) else { return .pathErr }
            guard let placeData: DetailModel = decodedData.data else { return .requestErr(decodedData.message) }
            return .success(placeData)
        }
}
